package com.jianbing.north.controller;

import com.jianbing.common.config.RuoYiConfig;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.utils.SecurityUtils;
import com.jianbing.common.utils.file.FileUploadUtils;
import com.jianbing.common.utils.file.FileUtils;
import com.jianbing.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/north")
public class NorthFileController {

    @Autowired
    private ServerConfig serverConfig;

    private String UPLOAD_PATH= RuoYiConfig.getProfile()+"/north";

    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception
    {
        try
        {
            String path = UPLOAD_PATH + "/"+SecurityUtils.getUsername();
            AjaxResult ajax = AjaxResult.success();
//            // 上传文件路径
//            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(path,file);
//            String url = serverConfig.getUrl() + fileName;
//            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

}
